package com.sm.li.controller;

import com.github.pagehelper.PageHelper;
import com.sm.li.common.http.AxiosResult;
import com.sm.li.common.page.PageBean;
import com.sm.li.common.perm.HasPerm;
import com.sm.li.common.valid.group.*;
import com.sm.li.controller.base.BaseController;
import com.sm.li.dto.MenuDTO;
import com.sm.li.entity.Menu;
import com.sm.li.query.MenuQuery;
import com.sm.li.service.MenuService;
import com.sm.li.transfer.MenuTransfer;
import com.sm.li.utils.FormValidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController extends BaseController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private MenuTransfer menuTransfer;

//    @GetMapping
//    public AxiosResult<List<Menu>> list() {
//        return AxiosResult.success(menuService.list());
//    }
    @GetMapping("{id}")
    public AxiosResult<MenuDTO> findById(@PathVariable Long id) {
        Menu byId = menuService.findById(id);
        MenuDTO menuDTO = menuTransfer.toDTO(byId);
        return AxiosResult.success(menuDTO);
    }

    //分页条件查询
    @GetMapping("searchPage")
    public AxiosResult<PageBean<MenuDTO>> searchPage(MenuQuery menuQuery) {
        //开启分页
        PageHelper.startPage(menuQuery.getCurrentPage(), menuQuery.getPageSize());
        PageBean<MenuDTO> pageBean = menuService.searchPage(menuQuery);
        return AxiosResult.success(pageBean);
    }

    //@Valid 开启后端表单校验
    @PostMapping
    @HasPerm(permSign = "system:menu:add")
    public AxiosResult add(@Validated(AddGroup.class) @RequestBody Menu menu) {
        Integer menuType=menu.getMenuType();
        if (menuType==1){
            //判断目录相关的内容
            FormValidUtils.valid(menu, DirectoryGroup.class);
        }
        if (menuType==2){
            //判断菜单相关的内容
            FormValidUtils.valid(menu, MenuGroup.class);

        }
        if (menuType==3){
            //判断按钮相关的内容
            FormValidUtils.valid(menu, BtnGroup.class);

        }
        return toAxios(menuService.add(menu));
    }

    @PutMapping
    @HasPerm(permSign = "system:menu:edit")
    public AxiosResult<Void> update(@Validated(UpdateGroup.class) @RequestBody Menu menu) {
        Integer menuType=menu.getMenuType();
        if (menuType==1){
            //判断目录相关的内容
            FormValidUtils.valid(menu, DirectoryGroup.class);
        }
        if (menuType==2){
            //判断菜单相关的内容
            FormValidUtils.valid(menu, MenuGroup.class);

        }
        if (menuType==3){
            //判断按钮相关的内容
            FormValidUtils.valid(menu, BtnGroup.class);

        }
        return toAxios(menuService.update(menu));

    }

    @DeleteMapping("{id}")
    @HasPerm(permSign = "system:menu:delete")
    public AxiosResult<Void> deleteById(@PathVariable Long id) {

      int row= menuService.cascadeDeleteChildren(id);

        return toAxios(row);

    }

    //获得select框中选择的内容
    @GetMapping("getAllMenuTreeData")

    public AxiosResult<List<MenuDTO>> getSelectTreeData(){
      List<MenuDTO> list=  menuService.getTreeData();
      return AxiosResult.success(list);
    }


}
